import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * @date 2018-09-10
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryDatabaseList, createDatabase as _createDatabase, editDatabase as _editDatabase, deleteDatabase as _deleteDatabase, selectTenantOk as _selectTenantOk, queryTenantList, handleDeleteTenant as _handleDeleteTenant } from "../services/databaseService";
export default {
  namespace: 'database',
  state: {
    databaseData: {},
    // 查询数据列表
    databaseId: undefined,
    // 数据库id
    tenantData: {},
    // 租户列表
    datasourceId: undefined,
    // 数据源id
    pagination: {},
    // 分页信息
    tenantPagination: {} // 租户分页信息

  },
  effects: {
    // 获取表格数据
    fetchTableList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchTableList(_ref, _ref2) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchTableList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(queryDatabaseList, payload);

            case 4:
              res = _context.sent;
              list = getResponse(res);

              if (!list) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  databaseData: list,
                  pagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchTableList);
    }),
    // 新建保存
    createDatabase: /*#__PURE__*/_regeneratorRuntime.mark(function createDatabase(_ref3, _ref4) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function createDatabase$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(_createDatabase, _objectSpread({}, payload));

            case 5:
              _context2.t1 = _context2.sent;
              res = (0, _context2.t0)(_context2.t1);

              if (!res) {
                _context2.next = 10;
                break;
              }

              _context2.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  databaseId: res.databaseId,
                  datasourceId: res.datasourceId
                }
              });

            case 10:
              return _context2.abrupt("return", res);

            case 11:
            case "end":
              return _context2.stop();
          }
        }
      }, createDatabase);
    }),
    // 编辑保存
    editDatabase: /*#__PURE__*/_regeneratorRuntime.mark(function editDatabase(_ref5, _ref6) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function editDatabase$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_editDatabase, _objectSpread({}, payload));

            case 4:
              result = _context3.sent;
              return _context3.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, editDatabase);
    }),
    // 删除数据库
    deleteDatabase: /*#__PURE__*/_regeneratorRuntime.mark(function deleteDatabase(_ref7, _ref8) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteDatabase$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_deleteDatabase, payload);

            case 4:
              result = _context4.sent;
              return _context4.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, deleteDatabase);
    }),
    // 查询租户
    handleSearchTenant: /*#__PURE__*/_regeneratorRuntime.mark(function handleSearchTenant(_ref9, _ref10) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function handleSearchTenant$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call, put = _ref10.put;
              _context5.next = 4;
              return call(queryTenantList, payload);

            case 4:
              res = _context5.sent;
              list = getResponse(res);

              if (!list) {
                _context5.next = 9;
                break;
              }

              _context5.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  tenantData: list,
                  tenantPagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, handleSearchTenant);
    }),
    // 添加租户
    selectTenantOk: /*#__PURE__*/_regeneratorRuntime.mark(function selectTenantOk(_ref11, _ref12) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function selectTenantOk$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              _context6.next = 4;
              return call(_selectTenantOk, _objectSpread({}, payload));

            case 4:
              result = _context6.sent;
              return _context6.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, selectTenantOk);
    }),
    // 删除租户
    handleDeleteTenant: /*#__PURE__*/_regeneratorRuntime.mark(function handleDeleteTenant(_ref13, _ref14) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function handleDeleteTenant$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call;
              _context7.next = 4;
              return call(_handleDeleteTenant, _objectSpread({}, payload));

            case 4:
              result = _context7.sent;
              return _context7.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, handleDeleteTenant);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};